from celery import Celery
from myapp.services.dependency_analyzer import ModelDependencyAnalyzer
from myapp.models.model_train_model import Training_Model
from myapp import db

# 假设已经配置了Celery
app = Celery('dependency_analysis')

@app.task
def analyze_all_model_dependencies():
    """分析所有模型的依赖关系"""
    analyzer = ModelDependencyAnalyzer()
    models = db.session.query(Training_Model).all()
    
    for model in models:
        try:
            analyzer.sync_dependencies_to_db(model.id)
            print(f"分析模型 {model.name} 依赖关系完成")
        except Exception as e:
            print(f"分析模型 {model.name} 依赖关系失败: {e}")

@app.task
def analyze_model_dependency(model_id: int):
    """分析单个模型的依赖关系"""
    analyzer = ModelDependencyAnalyzer()
    try:
        analyzer.sync_dependencies_to_db(model_id)
        return f"模型 {model_id} 依赖关系分析完成"
    except Exception as e:
        return f"模型 {model_id} 依赖关系分析失败: {e}"